class OnClass {
  constructor(name) {
    this.name = name;
  }

  on(e, fn) {
    if (!this[e]) {
      console.log("OnClass.on.e ::", e);
      this[e] = fn.bind(this);
      console.log("OnClass.on.this ::", this);
    } else {
      throw e + " 已经存在，不可再次绑定！";
    }
  }

  remove(e) {
    delete this[e];
  }

  trigger(e, ...args) {
    console.log("OnClass.trigger.this ::", this);
    console.log("OnClass.trigger.e ::", e);
    console.log("OnClass.trigger.args ::", args);
    console.log("OnClass.trigger.this[e] ::", this[e]);
    return this[e]();
  }
}

class User extends OnClass {
  constructor(name) {
    super(name);
  }
}

const Bob = new User("Bob");

// 绑定 on 的时候 不能用 箭头函数
Bob.on("say", function () {
  console.log(this);
  console.log("i'm", this.name, ".");
});

Bob.on("say", function () {
  console.log(this);
  console.log("i'm", this.name, ".");
});

Bob.trigger("say");
Bob.remove("say");

Bob.on("say", function () {
  console.log(this);
});
Bob.trigger("say");
Bob.on("say", function () {
  console.log(this);
  console.log("i'm", this.name, ".");
});
